home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / tools / gcc / gcc270_src.lha / gcc-2.7.0-amiga / config / sh / sh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-15  |  47.0 KB  |  1,853 lines

  1. /* Output routines for GCC for Hitachi Super-H.
  2.    Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.  */
  20.  
  21. /* Contributed by Steve Chamberlain (sac@cygnus.com).
  22.    Improved by Jim Wilson (wilson@cygnus.com).  */
  23.  
  24. #include "config.h"
  25.  
  26. #include <stdio.h>
  27.  
  28. #include "rtl.h"
  29. #include "tree.h"
  30. #include "flags.h"
  31. #include "insn-flags.h"
  32. #include "expr.h"
  33. #include "regs.h"
  34. #include "hard-reg-set.h"
  35. #include "output.h"
  36.  
  37. #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
  38. #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
  39.  
  40. /* ??? The pragma interrupt support will not work for SH3.  */
  41. /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
  42.    output code for the next function appropriate for an interrupt handler.  */
  43. int pragma_interrupt;
  44.  
  45. /* This is set by #pragma trapa, and is similar to the above, except that
  46.    the compiler doesn't emit code to preserve all registers.  */
  47. static int pragma_trapa;
  48.  
  49. /* This is used for communication between SETUP_INCOMING_VARARGS and
  50.    sh_expand_prologue.  */
  51. int current_function_anonymous_args;
  52.  
  53. /* Global variables from toplev.c and final.c that are used within, but
  54.    not declared in any header file.  */
  55. extern char *version_string;
  56. extern int *insn_addresses;
  57.  
  58. /* Global variables for machine-dependent things. */
  59.  
  60. /* Which cpu are we scheduling for.  */
  61. enum processor_type sh_cpu;
  62.  
  63. /* Saved operands from the last compare to use when we generate an scc
  64.    or bcc insn.  */
  65.  
  66. rtx sh_compare_op0;
  67. rtx sh_compare_op1;
  68.  
  69. /* Provides the class number of the smallest class containing
  70.    reg number.  */
  71.  
  72. int regno_reg_class[FIRST_PSEUDO_REGISTER] =
  73. {
  74.   R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  75.   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  76.   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  77.   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
  78.   GENERAL_REGS, PR_REGS, T_REGS, NO_REGS,
  79.   MAC_REGS, MAC_REGS,
  80. };
  81.  
  82. /* Provide reg_class from a letter such as appears in the machine
  83.    description.  */
  84.  
  85. enum reg_class reg_class_from_letter[] =
  86. {
  87.   /* a */ NO_REGS, /* b */ NO_REGS, /* c */ NO_REGS, /* d */ NO_REGS,
  88.   /* e */ NO_REGS, /* f */ NO_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
  89.   /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ PR_REGS,
  90.   /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
  91.   /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
  92.   /* u */ NO_REGS, /* v */ NO_REGS, /* w */ NO_REGS, /* x */ MAC_REGS,
  93.   /* y */ NO_REGS, /* z */ R0_REGS
  94. };
  95.  
  96. /* Print the operand address in x to the stream.  */
  97.  
  98. void
  99. print_operand_address (stream, x)
  100.      FILE *stream;
  101.      rtx x;
  102. {
  103.   switch (GET_CODE (x))
  104.     {
  105.     case REG:
  106.       fprintf (stream, "@%s", reg_names[REGNO (x)]);
  107.       break;
  108.  
  109.     case PLUS:
  110.       {
  111.     rtx base = XEXP (x, 0);
  112.     rtx index = XEXP (x, 1);
  113.  
  114.     switch (GET_CODE (index))
  115.       {
  116.       case CONST_INT:
  117.         fprintf (stream, "@(%d,%s)", INTVAL (index),
  118.              reg_names[REGNO (base)]);
  119.         break;
  120.  
  121.       case REG:
  122.         fprintf (stream, "@(r0,%s)",
  123.              reg_names[MAX (REGNO (base), REGNO (index))]);
  124.         break;
  125.  
  126.       default:
  127.         debug_rtx (x);
  128.         abort ();
  129.       }
  130.       }
  131.       break;
  132.  
  133.     case PRE_DEC:
  134.       fprintf (stream, "@-%s", reg_names[REGNO (XEXP (x, 0))]);
  135.       break;
  136.  
  137.     case POST_INC:
  138.       fprintf (stream, "@%s+", reg_names[REGNO (XEXP (x, 0))]);
  139.       break;
  140.  
  141.     default:
  142.       output_addr_const (stream, x);
  143.       break;
  144.     }
  145. }
  146.  
  147. /* Print operand x (an rtx) in assembler syntax to file stream
  148.    according to modifier code.
  149.  
  150.    '.'  print a .s if insn needs delay slot
  151.    '@'  print rte or rts depending upon pragma interruptness
  152.    '#'  output a nop if there is nothing to put in the delay slot
  153.    'O'  print a constant without the #
  154.    'R'  print the LSW of a dp value - changes if in little endian
  155.    'S'  print the MSW of a dp value - changes if in little endian
  156.    'T'  print the next word of a dp value - same as 'R' in big endian mode.  */
  157.  
  158. void
  159. print_operand (stream, x, code)
  160.      FILE *stream;
  161.      rtx x;
  162.      int code;
  163. {
  164.   switch (code)
  165.     {
  166.     case '.':
  167.       if (final_sequence
  168.       && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
  169.     fprintf (stream, ".s");
  170.       break;
  171.     case '@':
  172.       if (pragma_interrupt)
  173.     fprintf (stream, "rte");
  174.       else
  175.     fprintf (stream, "rts");
  176.       break;
  177.     case '#':
  178.       /* Output a nop if there's nothing in the delay slot.  */
  179.       if (dbr_sequence_length () == 0)
  180.     fprintf (stream, "\n\tnop");
  181.       break;
  182.     case 'O':
  183.       output_addr_const (stream, x);
  184.       break;
  185.     case 'R':
  186.       fputs (reg_names[REGNO (x) + LSW], (stream));
  187.       break;
  188.     case 'S':
  189.       fputs (reg_names[REGNO (x) + MSW], (stream));
  190.       break;
  191.     case 'T':
  192.       /* Next word of a double.  */
  193.       switch (GET_CODE (x))
  194.     {
  195.     case REG:
  196.       fputs (reg_names[REGNO (x) + 1], (stream));
  197.       break;
  198.     case MEM:
  199.       print_operand_address (stream,
  200.                  XEXP (adj_offsettable_operand (x, 4), 0));
  201.       break;
  202.     }
  203.       break;
  204.     default:
  205.       switch (GET_CODE (x))
  206.     {
  207.     case REG:
  208.       fputs (reg_names[REGNO (x)], (stream));
  209.       break;
  210.     case MEM:
  211.       output_address (XEXP (x, 0));
  212.       break;
  213.     default:
  214.       fputc ('#', stream);
  215.       output_addr_const (stream, x);
  216.       break;
  217.     }
  218.       break;
  219.     }
  220. }
  221.  
  222. /* Emit code to perform a block move.  Choose the best method.
  223.  
  224.    OPERANDS[0] is the destination.
  225.    OPERANDS[1] is the source.
  226.    OPERANDS[2] is the size.
  227.    OPERANDS[3] is the alignment safe to use.  */
  228.  
  229. int
  230. expand_block_move (operands)
  231.      rtx *operands;
  232. {
  233.   int align = INTVAL (operands[3]);
  234.   int constp = (GET_CODE (operands[2]) == CONST_INT);
  235.   int bytes = (constp ? INTVAL (operands[2]) : 0);
  236.  
  237.   /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
  238.      alignment, or if it isn't a multiple of 4 bytes, then fail.  */
  239.   if (! constp || align < 4 || (bytes % 4 != 0))
  240.     return 0;
  241.  
  242.   if (bytes < 64)
  243.     {
  244.       char entry[30];
  245.       tree entry_name;
  246.       rtx func_addr_rtx;
  247.       rtx r4 = gen_rtx (REG, SImode, 4);
  248.       rtx r5 = gen_rtx (REG, SImode, 5);
  249.  
  250.       sprintf (entry, "__movstrSI%d", bytes);
  251.       entry_name = get_identifier (entry);
  252.  
  253.       func_addr_rtx
  254.     = copy_to_mode_reg (Pmode,
  255.                 gen_rtx (SYMBOL_REF, Pmode,
  256.                      IDENTIFIER_POINTER (entry_name)));
  257.       emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
  258.       emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
  259.       emit_insn (gen_block_move_real (func_addr_rtx));
  260.       return 1;
  261.     }
  262.  
  263.   /* This is the same number of bytes as a memcpy call, but to a different
  264.      less common function name, so this will occasionally use more space.  */
  265.   if (! TARGET_SMALLCODE)
  266.     {
  267.       tree entry_name;
  268.       rtx func_addr_rtx;
  269.       int final_switch, while_loop;
  270.       rtx r4 = gen_rtx (REG, SImode, 4);
  271.       rtx r5 = gen_rtx (REG, SImode, 5);
  272.       rtx r6 = gen_rtx (REG, SImode, 6);
  273.  
  274.       entry_name = get_identifier ("__movstr");
  275.       func_addr_rtx
  276.     = copy_to_mode_reg (Pmode,
  277.                 gen_rtx (SYMBOL_REF, Pmode,
  278.                      IDENTIFIER_POINTER (entry_name)));
  279.       emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
  280.       emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
  281.  
  282.       /* r6 controls the size of the move.  16 is decremented from it
  283.      for each 64 bytes moved.  Then the negative bit left over is used
  284.      as an index into a list of move instructions.  e.g., a 72 byte move
  285.      would be set up with size(r6) = 14, for one iteration through the
  286.      big while loop, and a switch of -2 for the last part.  */
  287.  
  288.       final_switch = 16 - ((bytes / 4) % 16);
  289.       while_loop = ((bytes / 4) / 16 - 1) * 16;
  290.       emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
  291.       emit_insn (gen_block_lump_real (func_addr_rtx));
  292.       return 1;
  293.     }
  294.  
  295.   return 0;
  296. }
  297.  
  298. /* Prepare operands for a move define_expand; specifically, one of the
  299.    operands must be in a register.  */
  300.  
  301. int
  302. prepare_move_operands (operands, mode)
  303.      rtx operands[];
  304.      enum machine_mode mode;
  305. {
  306.   /* Copy the source to a register if both operands aren't registers.  */
  307.   if (! reload_in_progress && ! reload_completed
  308.       && ! register_operand (operands[0], mode)
  309.       && ! register_operand (operands[1], mode))
  310.     operands[1] = copy_to_mode_reg (mode, operands[1]);
  311.  
  312.   return 0;
  313. }
  314.  
  315. /* Prepare the operands for an scc instruction; make sure that the
  316.    compare has been done.  */
  317. rtx
  318. prepare_scc_operands (code)
  319.      enum rtx_code code;
  320. {
  321.   rtx t_reg = gen_rtx (REG, SImode, T_REG);
  322.   enum rtx_code oldcode = code;
  323.  
  324.   /* First need a compare insn.  */
  325.   switch (code)
  326.     {
  327.     case NE:
  328.       /* It isn't possible to handle this case.  */
  329.       abort ();
  330.     case LT:
  331.       code = GT;
  332.       break;
  333.     case LE:
  334.       code = GE;
  335.       break;
  336.     case LTU:
  337.       code = GTU;
  338.       break;
  339.     case LEU:
  340.       code = GEU;
  341.       break;
  342.     }
  343.   if (code != oldcode)
  344.     {
  345.       rtx tmp = sh_compare_op0;
  346.       sh_compare_op0 = sh_compare_op1;
  347.       sh_compare_op1 = tmp;
  348.     }
  349.  
  350.   sh_compare_op0 = force_reg (SImode, sh_compare_op0);
  351.   if (code != EQ && code != NE
  352.       && (sh_compare_op1 != const0_rtx
  353.       || code == GTU  || code == GEU || code == LTU || code == LEU))
  354.     sh_compare_op1 = force_reg (SImode, sh_compare_op1);
  355.  
  356.   emit_insn (gen_rtx (SET, VOIDmode, t_reg,
  357.               gen_rtx (code, SImode, sh_compare_op0,
  358.                    sh_compare_op1)));
  359.  
  360.   return t_reg;
  361. }
  362.  
  363. /* Called from the md file, set up the operands of a compare instruction.  */
  364.  
  365. void
  366. from_compare (operands, code)
  367.      rtx *operands;
  368.      int code;
  369. {
  370.   if (code != EQ && code != NE)
  371.     {
  372.       /* Force args into regs, since we can't use constants here.  */
  373.       sh_compare_op0 = force_reg (SImode, sh_compare_op0);
  374.       if (sh_compare_op1 != const0_rtx
  375.       || code == GTU  || code == GEU || code == LTU || code == LEU)
  376.     sh_compare_op1 = force_reg (SImode, sh_compare_op1);
  377.     }
  378.   operands[1] = sh_compare_op0;
  379.   operands[2] = sh_compare_op1;
  380. }
  381.  
  382. /* Functions to output assembly code.  */
  383.  
  384. /* Return a sequence of instructions to perform DI or DF move.
  385.  
  386.    Since the SH cannot move a DI or DF in one instruction, we have
  387.    to take care when we see overlapping source and dest registers.  */
  388.  
  389. char *
  390. output_movedouble (insn, operands, mode)
  391.      rtx insn;
  392.      rtx operands[];
  393.      enum machine_mode mode;
  394. {
  395.   rtx dst = operands[0];
  396.   rtx src = operands[1];
  397.  
  398.   if (GET_CODE (dst) == MEM
  399.       && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
  400.     return "mov.l    %T1,%0\n\tmov.l    %1,%0";
  401.  
  402.   if (register_operand (dst, mode)
  403.       && register_operand (src, mode))
  404.     {
  405.       if (REGNO (src) == MACH_REG)
  406.     return "sts    mach,%S0\n\tsts    macl,%R0";
  407.  
  408.       /* When mov.d r1,r2 do r2->r3 then r1->r2;
  409.          when mov.d r1,r0 do r1->r0 then r2->r1.  */
  410.  
  411.       if (REGNO (src) + 1 == REGNO (dst))
  412.     return "mov    %T1,%T0\n\tmov    %1,%0";
  413.       else
  414.     return "mov    %1,%0\n\tmov    %T1,%T0";
  415.     }
  416.   else if (GET_CODE (src) == CONST_INT)
  417.     {
  418.       if (INTVAL (src) < 0)
  419.     output_asm_insn ("mov    #-1,%S0", operands);
  420.       else
  421.     output_asm_insn ("mov    #0,%S0", operands);
  422.  
  423.       return "mov    %1,%R0";
  424.     }
  425.   else if (GET_CODE (src) == MEM)
  426.     {
  427.       int ptrreg = -1;
  428.       int dreg = REGNO (dst);
  429.       rtx inside = XEXP (src, 0);
  430.  
  431.       if (GET_CODE (inside) == REG)
  432.     ptrreg = REGNO (inside);
  433.       else if (GET_CODE (inside) == SUBREG)
  434.     ptrreg = REGNO (SUBREG_REG (inside)) + SUBREG_WORD (inside);
  435.       else if (GET_CODE (inside) == PLUS)
  436.     {
  437.       ptrreg = REGNO (XEXP (inside, 0));
  438.       /* ??? A r0+REG address shouldn't be possible here, because it isn't
  439.          an offsettable address.  Unfortunately, offsettable addresses use
  440.          QImode to check the offset, and a QImode offsettable address
  441.          requires r0 for the other operand, which is not currently
  442.          supported, so we can't use the 'o' constraint.
  443.          Thus we must check for and handle r0+REG addresses here.
  444.          We punt for now, since this is likely very rare.  */
  445.       if (GET_CODE (XEXP (inside, 1)) == REG)
  446.         abort ();
  447.     }
  448.       else if (GET_CODE (inside) == LABEL_REF)
  449.     return "mov.l    %1,%0\n\tmov.l    %1+4,%T0";
  450.       else if (GET_CODE (inside) == POST_INC)
  451.     return "mov.l    %1,%0\n\tmov.l    %1,%T0";
  452.       else
  453.     abort ();
  454.  
  455.       /* Work out the safe way to copy.  Copy into the second half first.  */
  456.       if (dreg == ptrreg)
  457.     return "mov.l    %T1,%T0\n\tmov.l    %1,%0";
  458.     }
  459.  
  460.   return "mov.l    %1,%0\n\tmov.l    %T1,%T0";
  461. }
  462.  
  463. /* Print an instruction which would have gone into a delay slot after
  464.    another instruction, but couldn't because the other instruction expanded
  465.    into a sequence where putting the slot insn at the end wouldn't work.  */
  466.  
  467. static void
  468. print_slot (insn)
  469.      rtx insn;
  470. {
  471.   final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
  472.  
  473.   INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
  474. }
  475.  
  476. /* We can't tell if we need a register as a scratch for the jump
  477.    until after branch shortening, and then it's too late to allocate a
  478.    register the 'proper' way.  These instruction sequences are rare
  479.    anyway, so to avoid always using a reg up from our limited set, we'll
  480.    grab one when we need one on output.  */
  481.  
  482. /* ??? Should fix compiler so that using a clobber scratch in jump
  483.    instructions works, and then this will be unnecessary.  */
  484.  
  485. char *
  486. output_far_jump (insn, op)
  487.      rtx insn;
  488.      rtx op;
  489. {
  490.   rtx thislab = gen_label_rtx ();
  491.  
  492.   /* Output the delay slot insn first if any.  */
  493.   if (dbr_sequence_length ())
  494.     print_slot (final_sequence);
  495.  
  496.   output_asm_insn ("mov.l    r13,@-r15", 0);
  497.   output_asm_insn ("mov.l    %O0,r13", &thislab);
  498.   output_asm_insn ("jmp    @r13", 0);
  499.   output_asm_insn ("mov.l    @r15+,r13", 0);
  500.   output_asm_insn (".align    2", 0);
  501.   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (thislab));
  502.   output_asm_insn (".long    %O0", &op);
  503.   return "";
  504. }
  505.  
  506. /* Local label counter, used for constants in the pool and inside
  507.    pattern branches.  */
  508.  
  509. static int lf = 100;
  510.  
  511. /* Output code for ordinary branches.  */
  512.  
  513. char *
  514. output_branch (logic, insn, operands)
  515.      int logic;
  516.      rtx insn;
  517.      rtx *operands;
  518. {
  519.   int label = lf++;
  520.  
  521.   switch (get_attr_length (insn))
  522.     {
  523.     case 2:
  524.       /* A branch with an unfilled delay slot.  */
  525.     case 4:
  526.       /* Simple branch in range -252..+258 bytes */
  527.       return logic ? "bt%.    %l0" : "bf%.    %l0";
  528.  
  529.     case 6:
  530.       /* A branch with an unfilled delay slot.  */
  531.     case 8:
  532.       /* Branch in range -4092..+4098 bytes.  */
  533.       {
  534.     /* The call to print_slot will clobber the operands.  */
  535.     rtx op0 = operands[0];
  536.  
  537.     /* If the instruction in the delay slot is annulled (true), then
  538.        there is no delay slot where we can put it now.  The only safe
  539.        place for it is after the label.  */
  540.  
  541.     if (final_sequence)
  542.       {
  543.         fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
  544.              INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
  545.              ? "" : ".s", label);
  546.         if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
  547.           print_slot (final_sequence);
  548.       }
  549.     else
  550.       fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
  551.  
  552.     output_asm_insn ("bra    %l0", &op0);
  553.     fprintf (asm_out_file, "\tnop\n");
  554.     fprintf (asm_out_file, "LF%d:\n", label);
  555.  
  556.     if (final_sequence
  557.         && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
  558.       print_slot (final_sequence);
  559.       }
  560.       return "";
  561.  
  562.     case 16:
  563.       /* A branch with an unfilled delay slot.  */
  564.     case 18:
  565.       /* Branches a long way away.  */
  566.       {
  567.     /* The call to print_slot will clobber the operands.  */
  568.     rtx op0 = operands[0];
  569.  
  570.     /* If the instruction in the delay slot is annulled (true), then
  571.        there is no delay slot where we can put it now.  The only safe
  572.        place for it is after the label.  */
  573.  
  574.     if (final_sequence)
  575.       {
  576.         fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
  577.              INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
  578.              ? "" : ".s", label);
  579.         if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
  580.           print_slot (final_sequence);
  581.       }
  582.     else
  583.       fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
  584.  
  585.     output_far_jump (insn, op0);
  586.     fprintf (asm_out_file, "LF%d:\n", label);
  587.  
  588.     if (final_sequence
  589.         && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
  590.       print_slot (final_sequence);
  591.       }
  592.       return "";
  593.     }
  594.   return "bad";
  595. }
  596.  
  597. /* A copy of the option structure defined in toplev.c.  */
  598.  
  599. struct option
  600. {
  601.   char *string;
  602.   int *variable;
  603.   int on_value;
  604. };
  605.  
  606. /* Output a single output option string NAME to FILE, without generating
  607.    lines longer than MAX.  */
  608.  
  609. static int
  610. output_option (file, sep, type, name, indent, pos, max)
  611.      FILE *file;
  612.      char *sep;
  613.      char *type;
  614.      char *name;
  615.      char *indent;
  616.      int pos;
  617.      int max;
  618. {
  619.   if (strlen (sep) + strlen (type) + strlen (name) + pos > max)
  620.     {
  621.       fprintf (file, indent);
  622.       return fprintf (file, "%s%s", type, name);
  623.     }
  624.   return pos + fprintf (file, "%s%s%s", sep, type, name);
  625. }
  626.  
  627. /* A copy of the target_switches variable in toplev.c.  */
  628.  
  629. static struct
  630. {
  631.   char *name;
  632.   int value;
  633. } m_options[] = TARGET_SWITCHES;
  634.  
  635. /* Output all options to the assembly language file.  */
  636.  
  637. static void
  638. output_options (file, f_options, f_len, W_options, W_len,
  639.         pos, max, sep, indent, term)
  640.      FILE *file;
  641.      struct option *f_options;
  642.      struct option *W_options;
  643.      int f_len, W_len;
  644.      int pos;
  645.      int max;
  646.      char *sep;
  647.      char *indent;
  648.      char *term;
  649. {
  650.   register int j;
  651.  
  652.   if (optimize)
  653.     pos = output_option (file, sep, "-O", "", indent, pos, max);
  654.   if (write_symbols != NO_DEBUG)
  655.     pos = output_option (file, sep, "-g", "", indent, pos, max);
  656.   if (profile_flag)
  657.     pos = output_option (file, sep, "-p", "", indent, pos, max);
  658.   if (profile_block_flag)
  659.     pos = output_option (file, sep, "-a", "", indent, pos, max);
  660.  
  661.   for (j = 0; j < f_len; j++)
  662.     if (*f_options[j].variable == f_options[j].on_value)
  663.       pos = output_option (file, sep, "-f", f_options[j].string,
  664.                indent, pos, max);
  665.  
  666.   for (j = 0; j < W_len; j++)
  667.     if (*W_options[j].variable == W_options[j].on_value)
  668.       pos = output_option (file, sep, "-W", W_options[j].string,
  669.                indent, pos, max);
  670.  
  671.   for (j = 0; j < sizeof m_options / sizeof m_options[0]; j++)
  672.     if (m_options[j].name[0] != '\0'
  673.     && m_options[j].value > 0
  674.     && ((m_options[j].value & target_flags)
  675.         == m_options[j].value))
  676.       pos = output_option (file, sep, "-m", m_options[j].name,
  677.                indent, pos, max);
  678.  
  679.   fprintf (file, term);
  680. }
  681.  
  682. /* Output to FILE the start of the assembler file.  */
  683.  
  684. void
  685. output_file_start (file, f_options, f_len, W_options, W_len)
  686.      FILE *file;
  687.      struct option *f_options;
  688.      struct option *W_options;
  689.      int f_len, W_len;
  690. {
  691.   register int pos;
  692.  
  693.   output_file_directive (file, main_input_filename);
  694.  
  695.   /* Switch to the data section so that the coffsem symbol and the
  696.      gcc2_compiled. symbol aren't in the text section.  */
  697.   data_section ();
  698.  
  699.   pos = fprintf (file, "\n! Hitachi SH cc1 (%s) arguments:", version_string);
  700.   output_options (file, f_options, f_len, W_options, W_len,
  701.           pos, 75, " ", "\n! ", "\n\n");
  702.  
  703.   if (TARGET_LITTLE_ENDIAN)
  704.     fprintf (file, "\t.little\n");
  705. }
  706.  
  707. /* Actual number of instructions used to make a shift by N.  */
  708. static char ashiftrt_insns[] =
  709.   { 0,1,2,3,4,5,8,8,8,8,8,8,8,8,8,8,2,3,4,5,8,8,8,8,8,8,8,8,8,8,8,2};
  710.  
  711. /* Left shift and logical right shift are the same.  */
  712. static char shift_insns[]    =
  713.   { 0,1,1,2,2,3,3,4,1,2,2,3,3,4,3,3,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
  714.  
  715. /* Individual shift amounts needed to get the above length sequences.
  716.    One bit right shifts clobber the T bit, so when possible, put one bit
  717.    shifts in the middle of the sequence, so the ends are eligible for
  718.    branch delay slots.  */
  719. static short shift_amounts[32][5] = {
  720.   {0}, {1}, {2}, {2, 1},
  721.   {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
  722.   {8}, {8, 1}, {8, 2}, {8, 1, 2},
  723.   {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
  724.   {16}, {16, 1}, {16, 2}, {16, 1, 2},
  725.   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
  726.   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
  727.   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
  728.  
  729. /* This is used in length attributes in sh.md to help compute the length
  730.    of arbitrary constant shift instructions.  */
  731.  
  732. int
  733. shift_insns_rtx (insn)
  734.      rtx insn;
  735. {
  736.   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
  737.   int shift_count = INTVAL (XEXP (set_src, 1));
  738.   enum rtx_code shift_code = GET_CODE (set_src);
  739.  
  740.   switch (shift_code)
  741.     {
  742.     case ASHIFTRT:
  743.       return ashiftrt_insns[shift_count];
  744.     case LSHIFTRT:
  745.     case ASHIFT:
  746.       return shift_insns[shift_count];
  747.     default:
  748.       abort();
  749.     }
  750. }
  751.  
  752. /* Return the cost of a shift.  */
  753.  
  754. int
  755. shiftcosts (x)
  756.      rtx x;
  757. {
  758.   int value = INTVAL (XEXP (x, 1));
  759.  
  760.   /* If shift by a non constant, then this will be expensive.  */
  761.   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
  762.     {
  763.       if (TARGET_SH3)
  764.     return 2;
  765.       /* If not an sh3 then we don't even have an instruction for it.  */
  766.       return 20;
  767.     }
  768.  
  769.   /* Otherwise, return the true cost in instructions.  */
  770.   if (GET_CODE (x) == ASHIFTRT)
  771.     return ashiftrt_insns[value];
  772.   else
  773.     return shift_insns[value];
  774. }
  775.  
  776. /* Return the cost of an AND operation.  */
  777.  
  778. int
  779. andcosts (x)
  780.      rtx x;
  781. {
  782.   int i;
  783.  
  784.   /* Anding with a register is a single cycle and instruction.  */
  785.   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
  786.     return 1;
  787.  
  788.   i = INTVAL (XEXP (x, 1));
  789.   /* These constants are single cycle extu.[bw] instructions.  */
  790.   if (i == 0xff || i == 0xffff)
  791.     return 1;
  792.   /* Constants that can be used in an and immediate instruction is a single
  793.      cycle, but this requires r0, so make it a little more expensive.  */
  794.   if (CONST_OK_FOR_L (i))
  795.     return 2;
  796.   /* Constants that can be loaded with a mov immediate and an and.
  797.      This case is probably unnecessary.  */
  798.   if (CONST_OK_FOR_I (i))
  799.     return 2;
  800.   /* Any other constants requires a 2 cycle pc-relative load plus an and.
  801.      This case is probably unnecessary.  */
  802.   return 3;
  803. }
  804.  
  805. /* Return the cost of a multiply.  */
  806. int
  807. multcosts (x)
  808.      rtx x;
  809. {
  810.   if (TARGET_SH2)
  811.     {
  812.       /* We have a mul insn, so we can never take more than the mul and the
  813.      read of the mac reg, but count more because of the latency and extra
  814.      reg usage.  */
  815.       if (TARGET_SMALLCODE)
  816.     return 2;
  817.       return 3;
  818.     }
  819.  
  820.   /* If we're aiming at small code, then just count the number of
  821.      insns in a multiply call sequence.  */
  822.   if (TARGET_SMALLCODE)
  823.     return 5;
  824.  
  825.   /* Otherwise count all the insns in the routine we'd be calling too.  */
  826.   return 20;
  827. }
  828.  
  829. /* Code to expand a shift.  */
  830.  
  831. void
  832. gen_ashift (type, n, reg)
  833.      int type;
  834.      int n;
  835.      rtx reg;
  836. {
  837.   /* Negative values here come from the shift_amounts array.  */
  838.   if (n < 0)
  839.     {
  840.       if (type == ASHIFT)
  841.     type = LSHIFTRT;
  842.       else
  843.     type = ASHIFT;
  844.       n = -n;
  845.     }
  846.  
  847.   switch (type)
  848.     {
  849.     case ASHIFTRT:
  850.       emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
  851.       break;
  852.     case LSHIFTRT:
  853.       if (n == 1)
  854.     emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
  855.       else
  856.     emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
  857.       break;
  858.     case ASHIFT:
  859.       emit_insn (gen_ashlsi3_k (reg, reg, GEN_INT (n)));
  860.       break;
  861.     }
  862. }
  863.  
  864. /* Output RTL to split a constant shift into its component SH constant
  865.    shift instructions.  */
  866.    
  867. /* ??? For SH3, should reject constant shifts when slower than loading the
  868.    shift count into a register?  */
  869.  
  870. int
  871. gen_shifty_op (code, operands)
  872.      int code;
  873.      rtx *operands;
  874. {
  875.   int value = INTVAL (operands[2]);
  876.   int max, i;
  877.  
  878.   if (value == 31)
  879.     {
  880.       if (code == LSHIFTRT)
  881.     {
  882.       emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
  883.       emit_insn (gen_movt (operands[0]));
  884.       return;
  885.     }
  886.       else if (code == ASHIFT)
  887.     {
  888.       /* There is a two instruction sequence for 31 bit left shifts,
  889.          but it requires r0.  */
  890.       if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
  891.         {
  892.           emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
  893.           emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
  894.           return;
  895.         }
  896.     }
  897.     }
  898.  
  899.   max = shift_insns[value];
  900.   for (i = 0; i < max; i++)
  901.     gen_ashift (code, shift_amounts[value][i], operands[0]);
  902. }
  903.  
  904. /* Output RTL for an arithmetic right shift.  */
  905.  
  906. /* ??? Rewrite to use super-optimizer sequences.  */
  907.  
  908. int
  909. expand_ashiftrt (operands)
  910.      rtx *operands;
  911. {
  912.   rtx wrk;
  913.   char func[18];
  914.   tree func_name;
  915.   int value;
  916.  
  917.   if (TARGET_SH3 && GET_CODE (operands[2]) != CONST_INT)
  918.     {
  919.       rtx count = copy_to_mode_reg (SImode, operands[2]);
  920.       emit_insn (gen_negsi2 (count, count));
  921.       emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
  922.       return 1;
  923.     }
  924.   if (GET_CODE (operands[2]) != CONST_INT)
  925.     return 0;
  926.  
  927.   value = INTVAL (operands[2]);
  928.  
  929.   if (value == 31)
  930.     {
  931.       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
  932.       return 1;
  933.     }
  934.   else if (value >= 16 && value <= 19)
  935.     {
  936.       wrk = gen_reg_rtx (SImode);
  937.       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
  938.       value -= 16;
  939.       while (value--)
  940.     gen_ashift (ASHIFTRT, 1, wrk);
  941.       emit_move_insn (operands[0], wrk);
  942.       return 1;
  943.     }
  944.   /* Expand a short sequence inline, longer call a magic routine.  */
  945.   else if (value <= 5)
  946.     {
  947.       wrk = gen_reg_rtx (SImode);
  948.       emit_move_insn (wrk, operands[1]);
  949.       while (value--)
  950.     gen_ashift (ASHIFTRT, 1, wrk);
  951.       emit_move_insn (operands[0], wrk);
  952.       return 1;
  953.     }
  954.  
  955.   wrk = gen_reg_rtx (Pmode);
  956.  
  957.   /* Load the value into an arg reg and call a helper.  */
  958.   emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
  959.   sprintf (func, "__ashiftrt_r4_%d", value);
  960.   func_name = get_identifier (func);
  961.   emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
  962.                 IDENTIFIER_POINTER (func_name)));
  963.   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
  964.   emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
  965.   return 1;
  966. }
  967.  
  968. /* The SH cannot load a large constant into a register, constants have to
  969.    come from a pc relative load.  The reference of a pc relative load
  970.    instruction must be less than 1k infront of the instruction.  This
  971.    means that we often have to dump a constant inside a function, and
  972.    generate code to branch around it.
  973.  
  974.    It is important to minimize this, since the branches will slow things
  975.    down and make things bigger.
  976.  
  977.    Worst case code looks like:
  978.  
  979.    mov.l L1,rn
  980.    bra   L2
  981.    nop
  982.    align
  983.    L1:   .long value
  984.    L2:
  985.    ..
  986.  
  987.    mov.l L3,rn
  988.    bra   L4
  989.    nop
  990.    align
  991.    L3:   .long value
  992.    L4:
  993.    ..
  994.  
  995.    We fix this by performing a scan before scheduling, which notices which
  996.    instructions need to have their operands fetched from the constant table
  997.    and builds the table.
  998.  
  999.    The algorithm is:
  1000.  
  1001.    scan, find an instruction which needs a pcrel move.  Look forward, find the
  1002.    last barrier which is within MAX_COUNT bytes of the requirement.
  1003.    If there isn't one, make one.  Process all the instructions between
  1004.    the find and the barrier.
  1005.  
  1006.    In the above example, we can tell that L3 is within 1k of L1, so
  1007.    the first move can be shrunk from the 3 insn+constant sequence into
  1008.    just 1 insn, and the constant moved to L3 to make:
  1009.  
  1010.    mov.l        L1,rn
  1011.    ..
  1012.    mov.l        L3,rn
  1013.    bra          L4
  1014.    nop
  1015.    align
  1016.    L3:.long value
  1017.    L4:.long value
  1018.  
  1019.    Then the second move becomes the target for the shortening process.  */
  1020.  
  1021. typedef struct
  1022. {
  1023.   rtx value;            /* Value in table.  */
  1024.   rtx label;            /* Label of value.  */
  1025.   enum machine_mode mode;    /* Mode of value.  */
  1026. } pool_node;
  1027.  
  1028. /* The maximum number of constants that can fit into one pool, since
  1029.    the pc relative range is 0...1020 bytes and constants are at least 4
  1030.    bytes long.  */
  1031.  
  1032. #define MAX_POOL_SIZE (1020/4)
  1033. static pool_node pool_vector[MAX_POOL_SIZE];
  1034. static int pool_size;
  1035.  
  1036. /* ??? If we need a constant in HImode which is the truncated value of a
  1037.    constant we need in SImode, we could combine the two entries thus saving
  1038.    two bytes.  Is this common enough to be worth the effort of implementing
  1039.    it?  */
  1040.  
  1041. /* ??? This stuff should be done at the same time that we shorten branches.
  1042.    As it is now, we must assume that all branches are the maximum size, and
  1043.    this causes us to almost always output constant pools sooner than
  1044.    necessary.  */
  1045.  
  1046. /* Add a constant to the pool and return its label.  */
  1047.  
  1048. static rtx
  1049. add_constant (x, mode)
  1050.      rtx x;
  1051.      enum machine_mode mode;
  1052. {
  1053.   int i;
  1054.   rtx lab;
  1055.  
  1056.   /* First see if we've already got it.  */
  1057.   for (i = 0; i < pool_size; i++)
  1058.     {
  1059.       if (x->code == pool_vector[i].value->code
  1060.       && mode == pool_vector[i].mode)
  1061.     {
  1062.       if (x->code == CODE_LABEL)
  1063.         {
  1064.           if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
  1065.         continue;
  1066.         }
  1067.       if (rtx_equal_p (x, pool_vector[i].value))
  1068.         return pool_vector[i].label;
  1069.     }
  1070.     }
  1071.  
  1072.   /* Need a new one.  */
  1073.   pool_vector[pool_size].value = x;
  1074.   lab = gen_label_rtx ();
  1075.   pool_vector[pool_size].mode = mode;
  1076.   pool_vector[pool_size].label = lab;
  1077.   pool_size++;
  1078.   return lab;
  1079. }
  1080.  
  1081. /* Output the literal table.  */
  1082.  
  1083. static void
  1084. dump_table (scan)
  1085.      rtx scan;
  1086. {
  1087.   int i;
  1088.   int need_align = 1;
  1089.  
  1090.   /* Do two passes, first time dump out the HI sized constants.  */
  1091.  
  1092.   for (i = 0; i < pool_size; i++)
  1093.     {
  1094.       pool_node *p = &pool_vector[i];
  1095.  
  1096.       if (p->mode == HImode)
  1097.     {
  1098.       if (need_align)
  1099.         {
  1100.           scan = emit_insn_after (gen_align_2 (), scan);
  1101.           need_align = 0;
  1102.         }
  1103.       scan = emit_label_after (p->label, scan);
  1104.       scan = emit_insn_after (gen_consttable_2 (p->value), scan);
  1105.     }
  1106.     }
  1107.  
  1108.   need_align = 1;
  1109.  
  1110.   for (i = 0; i < pool_size; i++)
  1111.     {
  1112.       pool_node *p = &pool_vector[i];
  1113.  
  1114.       switch (p->mode)
  1115.     {
  1116.     case HImode:
  1117.       break;
  1118.     case SImode:
  1119.       if (need_align)
  1120.         {
  1121.           need_align = 0;
  1122.           scan = emit_label_after (gen_label_rtx (), scan);
  1123.           scan = emit_insn_after (gen_align_4 (), scan);
  1124.         }
  1125.       scan = emit_label_after (p->label, scan);
  1126.       scan = emit_insn_after (gen_consttable_4 (p->value), scan);
  1127.       break;
  1128.     case DImode:
  1129.       if (need_align)
  1130.         {
  1131.           need_align = 0;
  1132.           scan = emit_label_after (gen_label_rtx (), scan);
  1133.           scan = emit_insn_after (gen_align_4 (), scan);
  1134.         }
  1135.       scan = emit_label_after (p->label, scan);
  1136.       scan = emit_insn_after (gen_consttable_8 (p->value), scan);
  1137.       break;
  1138.     default:
  1139.       abort ();
  1140.       break;
  1141.     }
  1142.     }
  1143.  
  1144.   scan = emit_insn_after (gen_consttable_end (), scan);
  1145.   scan = emit_barrier_after (scan);
  1146.   pool_size = 0;
  1147. }
  1148.  
  1149. /* Return non-zero if constant would be an ok source for a
  1150.    mov.w instead of a mov.l.  */
  1151.  
  1152. static int
  1153. hi_const (src)
  1154.      rtx src;
  1155. {
  1156.   return (GET_CODE (src) == CONST_INT
  1157.       && INTVAL (src) >= -32768
  1158.       && INTVAL (src) <= 32767);
  1159. }
  1160.  
  1161. /* Non-zero if the insn is a move instruction which needs to be fixed.  */
  1162.  
  1163. /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
  1164.    CONST_DOUBLE input value is CONST_OK_FOR_I.  For a SFmode move, we don't
  1165.    need to fix it if the input value is CONST_OK_FOR_I.  */
  1166.  
  1167. static int
  1168. broken_move (insn)
  1169.      rtx insn;
  1170. {
  1171.   if (GET_CODE (insn) == INSN
  1172.       && GET_CODE (PATTERN (insn)) == SET
  1173.       /* We can load any 8 bit value if we don't care what the high
  1174.      order bits end up as.  */
  1175.       && GET_MODE (SET_DEST (PATTERN (insn))) != QImode
  1176.       && CONSTANT_P (SET_SRC (PATTERN (insn)))
  1177.       && (GET_CODE (SET_SRC (PATTERN (insn))) != CONST_INT
  1178.       || ! CONST_OK_FOR_I (INTVAL (SET_SRC (PATTERN (insn))))))
  1179.     return 1;
  1180.  
  1181.   return 0;
  1182. }
  1183.  
  1184. /* Find the last barrier from insn FROM which is close enough to hold the
  1185.    constant pool.  If we can't find one, then create one near the end of
  1186.    the range.  */
  1187.  
  1188. /* ??? It would be good to put constant pool tables between a case jump and
  1189.    the jump table.  This fails for two reasons.  First, there is no
  1190.    barrier after the case jump.  This is a bug in the casesi pattern.
  1191.    Second, inserting the table here may break the mova instruction that
  1192.    loads the jump table address, by moving the jump table too far away.
  1193.    We fix that problem by never outputting the constant pool between a mova
  1194.    and its label.  */
  1195.  
  1196. static rtx
  1197. find_barrier (from)
  1198.      rtx from;
  1199. {
  1200.   int count_si = 0;
  1201.   int count_hi = 0;
  1202.   int found_hi = 0;
  1203.   int found_si = 0;
  1204.   rtx found_barrier = 0;
  1205.   rtx found_mova = 0;
  1206.  
  1207.   /* For HImode: range is 510, add 4 because pc counts from address of
  1208.      second instruction after this one, subtract 2 for the jump instruction
  1209.      that we may need to emit before the table.  This gives 512.
  1210.      For SImode: range is 1020, add 4 because pc counts from address of
  1211.      second instruction after this one, subtract 2 in case pc is 2 byte
  1212.      aligned, subtract 2 for the jump instruction that we may need to emit
  1213.      before the table.  This gives 1020.  */
  1214.   while (from && count_si < 1020 && count_hi < 512)
  1215.     {
  1216.       int inc;
  1217.  
  1218.       if (GET_CODE (from) == BARRIER)
  1219.     found_barrier = from;
  1220.  
  1221.       /* Count the length of this insn - we assume that all moves will
  1222.      be 2 bytes long, except the DImode/DFmode movess.  */
  1223.  
  1224.       if (broken_move (from))
  1225.     {
  1226.       rtx src = SET_SRC (PATTERN (from));
  1227.  
  1228.       if (hi_const (src))
  1229.         found_hi = 1;
  1230.       else
  1231.         found_si = 1;
  1232.       inc = (GET_MODE_SIZE (GET_MODE (src)) > 4) ? 4 : 2;
  1233.     }
  1234.       else
  1235.     inc = get_attr_length (from);
  1236.  
  1237.       if (GET_CODE (from) == INSN
  1238.       && GET_CODE (PATTERN (from)) == SET
  1239.       && GET_CODE (SET_SRC (PATTERN (from))) == UNSPEC
  1240.       && XINT (SET_SRC (PATTERN (from)), 1) == 1)
  1241.     found_mova = from;
  1242.       else if (GET_CODE (from) == JUMP_INSN
  1243.            && (GET_CODE (PATTERN (from)) == ADDR_VEC
  1244.            || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
  1245.     found_mova = 0;
  1246.  
  1247.       if (found_si)
  1248.     count_si += inc;
  1249.       if (found_hi)
  1250.     count_hi += inc;
  1251.       from = NEXT_INSN (from);
  1252.     }
  1253.  
  1254.   /* Insert the constant pool table before the mova instruction, to prevent
  1255.      the mova label reference from going out of range.  */
  1256.   if (found_mova)
  1257.     from = found_mova;
  1258.  
  1259.   if (! found_barrier)
  1260.     {
  1261.       /* We didn't find a barrier in time to dump our stuff,
  1262.      so we'll make one.  */
  1263.       rtx label = gen_label_rtx ();
  1264.  
  1265.       /* We went one instruction too far above.  */
  1266.       from = PREV_INSN (from);
  1267.       /* Walk back to be just before any jump or label.
  1268.      Putting it before a label reduces the number of times the branch
  1269.      around the constant pool table will be hit.  Putting it before
  1270.      a jump makes it more likely that the bra delay slot will be
  1271.      filled.  */
  1272.       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
  1273.          || GET_CODE (from) == CODE_LABEL)
  1274.     from = PREV_INSN (from);
  1275.  
  1276.       from = emit_jump_insn_after (gen_jump (label), from);
  1277.       JUMP_LABEL (from) = label;
  1278.       found_barrier = emit_barrier_after (from);
  1279.       emit_label_after (label, found_barrier);
  1280.     }
  1281.  
  1282.   return found_barrier;
  1283. }
  1284.  
  1285. /* Exported to toplev.c.
  1286.  
  1287.    Scan the function looking for move instructions which have to be changed to
  1288.    pc-relative loads and insert the literal tables.  */
  1289.  
  1290. void
  1291. machine_dependent_reorg (first)
  1292.      rtx first;
  1293. {
  1294.   rtx insn;
  1295.  
  1296.   for (insn = first; insn; insn = NEXT_INSN (insn))
  1297.     {
  1298.       if (broken_move (insn))
  1299.     {
  1300.       rtx scan;
  1301.       /* Scan ahead looking for a barrier to stick the constant table
  1302.          behind.  */
  1303.       rtx barrier = find_barrier (insn);
  1304.  
  1305.       /* Now find all the moves between the points and modify them.  */
  1306.       for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
  1307.         {
  1308.           if (broken_move (scan))
  1309.         {
  1310.           rtx pat = PATTERN (scan);
  1311.           rtx src = SET_SRC (pat);
  1312.           rtx dst = SET_DEST (pat);
  1313.           enum machine_mode mode = GET_MODE (dst);
  1314.           rtx lab;
  1315.           rtx newinsn;
  1316.           rtx newsrc;
  1317.  
  1318.           if (mode == SImode && hi_const (src))
  1319.             {
  1320.               int offset = 0;
  1321.  
  1322.               mode = HImode;
  1323.               while (GET_CODE (dst) == SUBREG)
  1324.             {
  1325.               offset += SUBREG_WORD (dst);
  1326.               dst = SUBREG_REG (dst);
  1327.             }
  1328.               dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
  1329.             }
  1330.  
  1331.           lab = add_constant (src, mode);
  1332.           newsrc = gen_rtx (MEM, mode,
  1333.                     gen_rtx (LABEL_REF, VOIDmode, lab));
  1334.           RTX_UNCHANGING_P (newsrc) = 1;
  1335.           newinsn = emit_insn_after (gen_rtx (SET, VOIDmode,
  1336.                               dst, newsrc), scan);
  1337.  
  1338.           delete_insn (scan);
  1339.           scan = newinsn;
  1340.         }
  1341.         }
  1342.       dump_table (barrier);
  1343.     }
  1344.     }
  1345. }
  1346.  
  1347. /* Dump out instruction addresses, which is useful for debugging the
  1348.    constant pool table stuff.  */
  1349.  
  1350. /* ??? This is unnecessary, and probably should be deleted.  This makes
  1351.    the insn_addresses declaration above unnecessary.  */
  1352.  
  1353. /* ??? The addresses printed by this routine for insns are nonsense for
  1354.    insns which are inside of a sequence where none of the inner insns have
  1355.    variable length.  This is because the second pass of shorten_branches
  1356.    does not bother to update them.  */
  1357.  
  1358. void
  1359. final_prescan_insn (insn, opvec, noperands)
  1360.      rtx insn;
  1361.      rtx *opvec;
  1362.      int noperands;
  1363. {
  1364.   if (TARGET_DUMPISIZE)
  1365.     fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
  1366. }
  1367.  
  1368. /* Dump out any constants accumulated in the final pass.  These will
  1369.    will only be labels.  */
  1370.  
  1371. char *
  1372. output_jump_label_table ()
  1373. {
  1374.   int i;
  1375.  
  1376.   if (pool_size)
  1377.     {
  1378.       fprintf (asm_out_file, "\t.align 2\n");
  1379.       for (i = 0; i < pool_size; i++)
  1380.     {
  1381.       pool_node *p = &pool_vector[i];
  1382.  
  1383.       ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
  1384.                      CODE_LABEL_NUMBER (p->label));
  1385.       output_asm_insn (".long    %O0", &p->value);
  1386.     }
  1387.       pool_size = 0;
  1388.     }
  1389.  
  1390.   return "";
  1391. }
  1392.  
  1393. /* A full frame looks like:
  1394.  
  1395.    arg-5
  1396.    arg-4
  1397.    [ if current_function_anonymous_args
  1398.    arg-3
  1399.    arg-2
  1400.    arg-1
  1401.    arg-0 ]
  1402.    saved-fp
  1403.    saved-r10
  1404.    saved-r11
  1405.    saved-r12
  1406.    saved-pr
  1407.    local-n
  1408.    ..
  1409.    local-1
  1410.    local-0        <- fp points here.  */
  1411.  
  1412. /* Number of bytes pushed for anonymous args, used to pass information
  1413.    between expand_prologue and expand_epilogue.  */
  1414.  
  1415. static int extra_push;
  1416.  
  1417. /* Adjust the stack and return the number of bytes taken to do it.  */
  1418.  
  1419. static void
  1420. output_stack_adjust (size)
  1421.      int size;
  1422. {
  1423.   if (size)
  1424.     {
  1425.       rtx val = GEN_INT (size);
  1426.       rtx insn;
  1427.  
  1428.       if (! CONST_OK_FOR_I (size))
  1429.     {
  1430.       rtx reg = gen_rtx (REG, SImode, 3);
  1431.       emit_insn (gen_movsi (reg, val));
  1432.       val = reg;
  1433.     }
  1434.  
  1435.       insn = gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, val);
  1436.       emit_insn (insn);
  1437.     }
  1438. }
  1439.  
  1440. /* Output RTL to push register RN onto the stack.  */
  1441.  
  1442. static void
  1443. push (rn)
  1444.      int rn;
  1445. {
  1446.   rtx x;
  1447.   x = emit_insn (gen_push (gen_rtx (REG, SImode, rn)));
  1448.   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
  1449.                gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
  1450. }
  1451.  
  1452. /* Output RTL to pop register RN from the stack.  */
  1453.  
  1454. static void
  1455. pop (rn)
  1456.      int rn;
  1457. {
  1458.   rtx x;
  1459.   x = emit_insn (gen_pop (gen_rtx (REG, SImode, rn)));
  1460.   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
  1461.                gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
  1462. }
  1463.  
  1464. /* Generate code to push the regs specified in the mask, and return
  1465.    the number of bytes the insns take.  */
  1466.  
  1467. static void
  1468. push_regs (mask)
  1469.      int mask;
  1470. {
  1471.   int i;
  1472.  
  1473.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1474.     if (mask & (1 << i))
  1475.       push (i);
  1476. }
  1477.  
  1478. /* Work out the registers which need to be saved, both as a mask and a
  1479.    count.
  1480.  
  1481.    If doing a pragma interrupt function, then push all regs used by the
  1482.    function, and if we call another function (we can tell by looking at PR),
  1483.    make sure that all the regs it clobbers are safe too.  */
  1484.  
  1485. static int
  1486. calc_live_regs (count_ptr)
  1487.      int *count_ptr;
  1488. {
  1489.   int reg;
  1490.   int live_regs_mask = 0;
  1491.   int count = 0;
  1492.  
  1493.   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
  1494.     {
  1495.       if (pragma_interrupt && ! pragma_trapa)
  1496.     {
  1497.       /* Need to save all the regs ever live.  */
  1498.       if ((regs_ever_live[reg]
  1499.            || (call_used_regs[reg] && regs_ever_live[PR_REG]))
  1500.           && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
  1501.           && reg != T_REG && reg != GBR_REG)
  1502.         {
  1503.           live_regs_mask |= 1 << reg;
  1504.           count++;
  1505.         }
  1506.     }
  1507.       else
  1508.     {
  1509.       /* Only push those regs which are used and need to be saved.  */
  1510.       if (regs_ever_live[reg] && ! call_used_regs[reg])
  1511.         {
  1512.           live_regs_mask |= (1 << reg);
  1513.           count++;
  1514.         }
  1515.     }
  1516.     }
  1517.  
  1518.   *count_ptr = count;
  1519.   return live_regs_mask;
  1520. }
  1521.  
  1522. /* Code to generate prologue and epilogue sequences */
  1523.  
  1524. void
  1525. sh_expand_prologue ()
  1526. {
  1527.   int live_regs_mask;
  1528.   int d, i;
  1529.   live_regs_mask = calc_live_regs (&d);
  1530.  
  1531.   /* We have pretend args if we had an object sent partially in registers
  1532.      and partially on the stack, e.g. a large structure.  */
  1533.   output_stack_adjust (-current_function_pretend_args_size);
  1534.  
  1535.   extra_push = 0;
  1536.  
  1537.   /* This is set by SETUP_VARARGS to indicate that this is a varargs
  1538.      routine.  Clear it here so that the next function isn't affected.  */
  1539.   if (current_function_anonymous_args)
  1540.     {
  1541.       current_function_anonymous_args = 0;
  1542.  
  1543.       /* Push arg regs as if they'd been provided by caller in stack.  */
  1544.       for (i = 0; i < NPARM_REGS; i++)
  1545.     {
  1546.       int rn = NPARM_REGS + FIRST_PARM_REG - i - 1;
  1547.       if (i > (NPARM_REGS - current_function_args_info
  1548.            - current_function_varargs))
  1549.         break;
  1550.       push (rn);
  1551.       extra_push += 4;
  1552.     }
  1553.     }
  1554.   push_regs (live_regs_mask);
  1555.   output_stack_adjust (-get_frame_size ());
  1556.  
  1557.   if (frame_pointer_needed)
  1558.     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
  1559. }
  1560.  
  1561. void
  1562. sh_expand_epilogue ()
  1563. {
  1564.   int live_regs_mask;
  1565.   int d, i;
  1566.  
  1567.   live_regs_mask = calc_live_regs (&d);
  1568.  
  1569.   if (frame_pointer_needed)
  1570.     emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
  1571.  
  1572.   output_stack_adjust (get_frame_size ());
  1573.  
  1574.   /* Pop all the registers.  */
  1575.  
  1576.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1577.     {
  1578.       int j = (FIRST_PSEUDO_REGISTER - 1) - i;
  1579.       if (live_regs_mask & (1 << j))
  1580.     pop (j);
  1581.     }
  1582.  
  1583.   output_stack_adjust (extra_push + current_function_pretend_args_size);
  1584. }
  1585.  
  1586. /* Clear variables at function end.  */
  1587.  
  1588. void
  1589. function_epilogue (stream, size)
  1590.      FILE *stream;
  1591.      int size;
  1592. {
  1593.   pragma_interrupt = pragma_trapa = 0;
  1594. }
  1595.  
  1596. /* Define the offset between two registers, one to be eliminated, and
  1597.    the other its replacement, at the start of a routine.  */
  1598.  
  1599. int
  1600. initial_elimination_offset (from, to)
  1601.      int from;
  1602.      int to;
  1603. {
  1604.   int regs_saved;
  1605.   int total_saved_regs_space;
  1606.   int total_auto_space = get_frame_size ();
  1607.  
  1608.   calc_live_regs (®s_saved);
  1609.   total_saved_regs_space = (regs_saved) * 4;
  1610.  
  1611.   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
  1612.     return total_saved_regs_space + total_auto_space;
  1613.  
  1614.   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
  1615.     return total_saved_regs_space + total_auto_space;
  1616.  
  1617.   /* Initial gap between fp and sp is 0.  */
  1618.   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
  1619.     return 0;
  1620.  
  1621.   abort ();
  1622. }
  1623.  
  1624. /* Handle machine specific pragmas to be semi-compatible with Hitachi
  1625.    compiler.  */
  1626.  
  1627. int
  1628. handle_pragma (file)
  1629.      FILE *file;
  1630. {
  1631.   int c;
  1632.   char pbuf[200];
  1633.   int psize = 0;
  1634.  
  1635.   c = getc (file);
  1636.   while (c == ' ' || c == '\t')
  1637.     c = getc (file);
  1638.  
  1639.   if (c == '\n' || c == EOF)
  1640.     return c;
  1641.  
  1642.   while (psize < sizeof (pbuf) - 1 && c != '\n')
  1643.     {
  1644.       pbuf[psize++] = c;
  1645.       if (psize == 9 && strncmp (pbuf, "interrupt", 9) == 0)
  1646.     {
  1647.       pragma_interrupt = 1;
  1648.       return ' ';
  1649.     }
  1650.       if (psize == 5 && strncmp (pbuf, "trapa", 5) == 0)
  1651.     {
  1652.       pragma_interrupt = pragma_trapa = 1;
  1653.       return ' ';
  1654.     }
  1655.       c = getc (file);
  1656.     }
  1657.   return c;
  1658. }
  1659.  
  1660. /* Predicates used by the templates.  */
  1661.  
  1662. /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
  1663.    Used only in general_movsrc_operand.  */
  1664.  
  1665. int
  1666. system_reg_operand (op, mode)
  1667.      rtx op;
  1668.      enum machine_mode mode;
  1669. {
  1670.   switch (REGNO (op))
  1671.     {
  1672.     case PR_REG:
  1673.     case MACL_REG:
  1674.     case MACH_REG:
  1675.       return 1;
  1676.     }
  1677.   return 0;
  1678. }
  1679.  
  1680. /* Returns 1 if OP can be source of a simple move operation.
  1681.    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
  1682.    invalid as are subregs of system registers.  */
  1683.  
  1684. int
  1685. general_movsrc_operand (op, mode)
  1686.      rtx op;
  1687.      enum machine_mode mode;
  1688. {
  1689.   if (GET_CODE (op) == MEM)
  1690.     {
  1691.       rtx inside = XEXP (op, 0);
  1692.       if (GET_CODE (inside) == CONST)
  1693.     inside = XEXP (inside, 0);
  1694.  
  1695.       if (GET_CODE (inside) == LABEL_REF)
  1696.     return 1;
  1697.  
  1698.       if (GET_CODE (inside) == PLUS
  1699.       && GET_CODE (XEXP (inside, 0)) == LABEL_REF
  1700.       && GET_CODE (XEXP (inside, 1)) == CONST_INT)
  1701.     return 1;
  1702.  
  1703.       /* Only post inc allowed.  */
  1704.       if (GET_CODE (inside) == PRE_DEC)
  1705.     return 0;
  1706.     }
  1707.  
  1708.   if ((mode == QImode || mode == HImode)
  1709.       && (GET_CODE (op) == SUBREG
  1710.       && GET_CODE (XEXP (op, 0)) == REG
  1711.       && system_reg_operand (XEXP (op, 0), mode)))
  1712.     return 0;
  1713.  
  1714.   return general_operand (op, mode);
  1715. }
  1716.  
  1717. /* Returns 1 if OP can be a destination of a move.
  1718.    Same as general_operand, but no preinc allowed.  */
  1719.  
  1720. int
  1721. general_movdst_operand (op, mode)
  1722.      rtx op;
  1723.      enum machine_mode mode;
  1724. {
  1725.   /* Only pre dec allowed.  */
  1726.   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
  1727.     return 0;
  1728.  
  1729.   return general_operand (op, mode);
  1730. }
  1731.  
  1732. /* Returns 1 if OP is a normal arithmetic register.  */
  1733.  
  1734. int
  1735. arith_reg_operand (op, mode)
  1736.      rtx op;
  1737.      enum machine_mode mode;
  1738. {
  1739.   if (register_operand (op, mode))
  1740.     {
  1741.       if (GET_CODE (op) == REG)
  1742.     return (REGNO (op) != T_REG
  1743.         && REGNO (op) != PR_REG
  1744.         && REGNO (op) != MACH_REG
  1745.         && REGNO (op) != MACL_REG);
  1746.       return 1;
  1747.     }
  1748.   return 0;
  1749. }
  1750.  
  1751. /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
  1752.  
  1753. int
  1754. arith_operand (op, mode)
  1755.      rtx op;
  1756.      enum machine_mode mode;
  1757. {
  1758.   if (arith_reg_operand (op, mode))
  1759.     return 1;
  1760.  
  1761.   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
  1762.     return 1;
  1763.  
  1764.   return 0;
  1765. }
  1766.  
  1767. /* Returns 1 if OP is a valid source operand for a compare insn.  */
  1768.  
  1769. int
  1770. arith_reg_or_0_operand (op, mode)
  1771.      rtx op;
  1772.      enum machine_mode mode;
  1773. {
  1774.   if (arith_reg_operand (op, mode))
  1775.     return 1;
  1776.  
  1777.   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
  1778.     return 1;
  1779.  
  1780.   return 0;
  1781. }
  1782.  
  1783. /* Returns 1 if OP is a valid source operand for a logical operation.  */
  1784.  
  1785. int
  1786. logical_operand (op, mode)
  1787.      rtx op;
  1788.      enum machine_mode mode;
  1789. {
  1790.   if (arith_reg_operand (op, mode))
  1791.     return 1;
  1792.  
  1793.   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
  1794.     return 1;
  1795.  
  1796.   return 0;
  1797. }
  1798.  
  1799. /* Determine where to put an argument to a function.
  1800.    Value is zero to push the argument on the stack,
  1801.    or a hard register in which to store the argument.
  1802.  
  1803.    MODE is the argument's machine mode.
  1804.    TYPE is the data type of the argument (as a tree).
  1805.     This is null for libcalls where that information may
  1806.     not be available.
  1807.    CUM is a variable of type CUMULATIVE_ARGS which gives info about
  1808.     the preceding args and about the function being called.
  1809.    NAMED is nonzero if this argument is a named parameter
  1810.     (otherwise it is an extra parameter matching an ellipsis).  */
  1811.  
  1812. rtx
  1813. sh_function_arg (cum, mode, type, named)
  1814.      CUMULATIVE_ARGS cum;
  1815.      enum machine_mode mode;
  1816.      tree type;
  1817.      int named;
  1818. {
  1819.   if (named)
  1820.     {
  1821.       int rr = (ROUND_REG (cum, mode));
  1822.  
  1823.       if (rr < NPARM_REGS)
  1824.     return ((type == 0 || ! TREE_ADDRESSABLE (type))
  1825.         ? gen_rtx (REG, mode, FIRST_PARM_REG + rr) : 0);
  1826.     }
  1827.   return 0;
  1828. }
  1829.  
  1830. /* For an arg passed partly in registers and partly in memory,
  1831.    this is the number of registers used.
  1832.    For args passed entirely in registers or entirely in memory, zero.
  1833.    Any arg that starts in the first 4 regs but won't entirely fit in them
  1834.    needs partial registers on the SH.  */
  1835.  
  1836. int
  1837. sh_function_arg_partial_nregs (cum, mode, type, named)
  1838.      CUMULATIVE_ARGS cum;
  1839.      enum machine_mode mode;
  1840.      tree type;
  1841.      int named;
  1842. {
  1843.   if (cum < NPARM_REGS)
  1844.     {
  1845.       if ((type == 0 || ! TREE_ADDRESSABLE (type))
  1846.       && (cum + (mode == BLKmode
  1847.              ? ROUND_ADVANCE (int_size_in_bytes (type))
  1848.              : ROUND_ADVANCE (GET_MODE_SIZE (mode))) - NPARM_REGS > 0))
  1849.     return NPARM_REGS - cum;
  1850.     }
  1851.   return 0;
  1852. }
  1853.